your#!/bin/bash

echo "========================================"
echo "OffGrid 场馆管理功能部署脚本"
echo "========================================"
echo

# 检查是否在backend目录
if [ ! -f "package.json" ]; then
    echo "错误：请在backend目录下运行此脚本"
    exit 1
fi

echo "[1/5] 检查环境..."
if ! command -v node &> /dev/null; then
    echo "错误：Node.js未安装"
    exit 1
fi

if ! command -v npm &> /dev/null; then
    echo "错误：npm未安装"
    exit 1
fi

echo "[2/5] 安装依赖..."
npm install
if [ $? -ne 0 ]; then
    echo "错误：依赖安装失败"
    exit 1
fi

echo "[3/5] 生成Prisma客户端..."
npx prisma generate
if [ $? -ne 0 ]; then
    echo "错误：Prisma客户端生成失败"
    exit 1
fi

echo "[4/5] 执行数据库迁移..."
echo "注意：请确保数据库连接正常"
echo "执行SQL脚本：scripts/add-venue-table.sql"
echo
echo "请手动执行以下命令："
echo "mysql -u your_username -p your_database < scripts/add-venue-table.sql"
echo
echo "或者使用Prisma迁移："
echo "npx prisma migrate dev --name add_venue_table"
echo

echo "[5/5] 启动服务..."
echo "场馆管理功能部署完成！"
echo
echo "可用的API接口："
echo "- 管理后台：/api/admin/venues/*"
echo "- 客户端：/api/client/venues/*"
echo
echo "启动服务命令："
echo "npm run start:dev"
echo

# 询问是否立即启动服务
read -p "是否立即启动服务？(y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "启动服务..."
    npm run start:dev
fi
